package com.example.actordemo1.tools;

import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import scala.concurrent.duration.Duration;

import java.util.concurrent.TimeUnit;


//定时
public class SchedulerActor extends AbstractActor {
    @Override
    public Receive createReceive() {
        return receiveBuilder().matchAny(o->{
            System.out.println(getContext().parent()+" ............."+self().path()+o);
        }).build();
    }

    public static void main(String[] args) {

        ActorSystem actorSystem = ActorSystem.create("sys");
        ActorRef actorRef = actorSystem.actorOf(Props.create(SchedulerActor.class), "schedulerActor");


        actorSystem.scheduler().scheduleOnce(Duration.create(3, TimeUnit.SECONDS), new Runnable() {
            @Override
            public void run() {
                actorRef.tell("qqqq",ActorRef.noSender());
            }
        },actorSystem.dispatcher());

        actorSystem.scheduler().scheduleOnce(Duration.create(5, TimeUnit.SECONDS),
                actorRef,"wwww",actorSystem.dispatcher(),ActorRef.noSender());

        actorSystem.scheduler().schedule(Duration.create(2, TimeUnit.SECONDS),
                Duration.create(3, TimeUnit.SECONDS), actorRef,"定时",actorSystem.dispatcher(),
                ActorRef.noSender());

    }
}
